﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RH.Domain
{
    /// <summary>
    /// This Call Will Be Used to Contain the Extension Methods of Entity Context Class
    /// </summary>
    public static class ContextExtensions
    {
        /// <summary>
        /// Determines whether the underlying database connection is exist and operational
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static bool IsConnected(this AppEntities context)
        {
            try
            {
                //var entityConnection = context.Connection as System.Data.EntityClient.EntityConnection;
                int count = context.Countries.Count();
                //if (entityConnection != null)
                //{
                //    if (entityConnection.State == System.Data.ConnectionState.Open)
                //        return true;                    
                //}                
                return true;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return false;
        }
        /// <summary>
        /// Checks Whethear a Given Email is Exist in some certain tables [User, Contact, Comapny]
        /// <para>
        /// This is Applicable Only when a new Entity is Going to be created.
        /// In Update Mode this method should not be used. Otherwise it will create cirtical bug.        
        /// </para>
        /// </summary>
        /// <param name="context"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsDuplicateEmail(this AppEntities context, String email)
        {
            if (context.Users.FirstOrDefault(P => String.Compare(P.UserName, email, true) == 0) != null)
                return true;
            //else if (context.Contacts.FirstOrDefault(P => String.Compare(P.EmailPersonal, email, true) == 0 || String.Compare(P.EmailOffice, email, true) == 0) != null)
            //    return true;
            else if (context.Addresses.FirstOrDefault(P => String.Compare(P.Email, email, true) == 0 || String.Compare(P.EmailOffice, email, true) == 0) != null)
                return true;

            return false;
        }
        /// <summary>
        /// Checks Whethear a Company Name is Duplicate or not
        /// In Update Mode Company ID should be Supplied
        /// </summary>
        /// <param name="context"></param>
        /// <param name="companyName"></param>
        /// <param name="companyID"></param>
        /// <returns></returns>
        public static bool IsDuplicateCompanyName(this AppEntities context, string companyName, int companyID = 0)
        {
            if (companyID == 0)
            {
                if (context.Companies.FirstOrDefault(P => String.Compare(P.Name, companyName, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Companies.FirstOrDefault(P => P.ID != companyID && String.Compare(P.Name, companyName, true) == 0) != null)
                    return true;
            }
            return false;
        }
        /// <summary>
        /// Checks whethear a UserName is already taken or not.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userName"></param>
        /// <param name="userID">The User ID that will be excluded for duplicate checking. Pass 0 to check in all records</param>
        /// <returns></returns>
        public static bool IsDuplicateUserName(this AppEntities context, string userName, int userID)
        {
            if (userID == 0)
            {
                if (context.Users.FirstOrDefault(P => String.Compare(P.UserName, userName, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Users.FirstOrDefault(P => P.ID != userID && String.Compare(P.UserName, userName, true) == 0) != null)
                    return true;
            }
            return false;
        }
        ///// <summary>
        ///// Checks Whether a Code is Duplicate or Not
        ///// </summary>
        ///// <param name="context"></param>
        ///// <param name="productCode"></param>
        ///// <param name="productID"></param>
        ///// <returns></returns>
        //public static bool IsDuplicateProductCode(this AppEntities context, string productCode, int productID = 0)
        //{
        //    if (productID == 0)
        //    {
        //        if (context.Products.FirstOrDefault(P => String.Compare(P.Code, productCode, true) == 0) != null)
        //            return true;
        //    }
        //    else
        //    {
        //        if (context.Products.FirstOrDefault(P => P.ID != productID && String.Compare(P.Code, productCode, true) == 0) != null)
        //            return true;
        //    }
        //    return false;
        //}

        /// <summary>
        /// Checks for Duplicate Product UPC Code
        /// </summary>
        /// <param name="context"></param>
        /// <param name="productUPC"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        //public static bool IsDuplicateProductUPC(this AppEntities context, string productUPC, int productID = 0)
        //{
        //    if (productID == 0)
        //    {
        //        if (context.Products.FirstOrDefault(P => String.Compare(P.UPC, productUPC, true) == 0) != null)
        //            return true;
        //    }
        //    else
        //    {
        //        if (context.Products.FirstOrDefault(P => P.ID != productID && String.Compare(P.UPC, productUPC, true) == 0) != null)
        //            return true;
        //    }
        //    return false;
        //}

        /// <summary>
        /// Checks Whether a Product Title->Url is Duplicate or Not
        /// </summary>
        /// <param name="context"></param>
        /// <param name="productCode"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        public static bool IsDuplicateProductUrl(this AppEntities context, string productUrl, int productID = 0)
        {
            if (productID == 0)
            {
                if (context.Products.FirstOrDefault(P => String.Compare(P.ProductUrl, productUrl, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Products.FirstOrDefault(P => P.ID != productID && String.Compare(P.ProductUrl, productUrl, true) == 0) != null)
                    return true;
            }
            return false;
        }
        /// <summary>
        /// Verifies a Registration request in Second Step.
        /// Whethear it is a valid request or not.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="companyID"></param>
        /// <returns></returns>
        public static bool IsValidRegistrationRequest(this AppEntities context, int companyID)
        {
            Company company = context.Companies.Include("Contacts").FirstOrDefault(P => P.ID == companyID);            
            if (company == null || company.IsActive || company.Contacts.Count > 0)
                return false;

            return true;
        }

        /// <summary>
        /// Checks Whether the content title is already exist or not
        /// </summary>
        /// <param name="context"></param>
        /// <param name="contentUrl"></param>
        /// <param name="contentID"></param>
        /// <returns></returns>
        public static bool IsDuplicateContent(this AppEntities context, string contentUrl, int contentID = 0)
        {
            if (contentID == 0)
            {
                if (context.Contents.FirstOrDefault(P => String.Compare(P.Url, contentUrl, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Contents.FirstOrDefault(P => P.ID != contentID && String.Compare(P.Url, contentUrl, true) == 0) != null)
                    return true;
            }
            return false;
        }
        /// <summary>
        /// Checks whether a category URL is duplicate or not
        /// </summary>
        /// <param name="context"></param>
        /// <param name="categoryUrl"></param>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static bool IsDuplicateCategory(this AppEntities context, string categoryUrl, int categoryID = 0)
        {            
            if (categoryID == 0)
            {
                if (context.Categories.FirstOrDefault(P => String.Compare(P.UrlName, categoryUrl, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Categories.FirstOrDefault(P => P.ID != categoryID && String.Compare(P.UrlName, categoryUrl, true) == 0) != null)
                    return true;
            }
            return false;
        }
    }
}
